package leetcode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 描述:1047. 删除字符串中的所有相邻重复项
 *
 * @author Madison You
 * @created 23:32
 */
public class Easy_1047_RemoveDuplicates {
    public static void main(String[] args) {
        System.out.println(new Easy_1047_RemoveDuplicates().removeDuplicates("abbaca"));;
    }

    public String removeDuplicates(String s) {
        Deque<Character> deque = new LinkedList();
        for (int i = 0; i < s.length(); i++) {
            if (!deque.isEmpty()) {
                Character peek = deque.peek();
                if (peek == s.charAt(i)) {
                    deque.pop();
                } else {
                    deque.push(s.charAt(i));
                }
            } else {
                deque.push(s.charAt(i));
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!deque.isEmpty()) {
            sb.append(deque.pollLast());
        }
        return sb.toString();
    }
}
